package edu.cmu.fithub.formbeans;

import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import org.mybeans.form.FileProperty;
import org.mybeans.form.FormBean;

public class UpdateProfileForm extends FormBean {

	private String password;
	private String passwordConf;
	private String age;
	private String firstName;
	private String lastName;
	private String timeZone;
	private String email;
	private String weight;
	private FileProperty image;
	private String action;
	
	public String getPasswordConf() {
		return passwordConf;
	}

	public void setPasswordConf(String passwordConf) {
		this.passwordConf = passwordConf;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}


	public String getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(String timeZone) {
		this.timeZone = timeZone;
	}

	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public List<String> getValidationErrors() {
		List<String> errors = new ArrayList<String>();

		if (email != null && email.matches(".*[<>].*")) {
			errors.add("Invalid Email");
		}

		if (firstName != null && firstName.matches(".*[<>\"].*")) {
			errors.add("First Name may not contain angle brackets or quotes");
		}
		if (lastName != null && lastName.matches(".*[<>\"].*")) {
			errors.add("Last Name may not contain angle brackets or quotes");
		}
		
		if (age != null && age.length() != 0 && !age.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
			errors.add("Age must be a valid number");
		}
		
		if (weight != null && weight.length() != 0 && !weight.matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+")) {
			errors.add("Weight must be a valid number");
		}
		
		if (password != null && password.length() != 0 && password.matches(".*[<>].*")) {
			errors.add("Password may not contain angle brackets");
			password = "";
			passwordConf = "";
		}
		if (password != null && password.length() != 0 && !password.equals(passwordConf)) {
			errors.add("Passwords do not match");
			password = "";
			passwordConf = "";
		}
		if (timeZone != null && timeZone.length() != 0 && timeZone.length() != 0) {
			TimeZone tZone = TimeZone.getTimeZone(timeZone.trim());
			if ((!timeZone.equals("GMT") && (tZone.getID().equals("GMT")))) {
				errors.add("Time Zone is invalid");
			}
		}
		
		if (image.getBytes().length > 65000) {
			errors.add("Image too Big");
		}
		
		return errors;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public FileProperty getImage() {
		return image;
	}

	public void setImage(FileProperty image) {
		this.image = image;
	}
}
